//
//  MCMusicPlayVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/31.
//

import UIKit
import JXSegmentedView
import QMUIKit
import SnapKit
import Toast_Swift
import ObjectMapper

typealias musicPlayInfoLastBlock = (_ musicPlay:MCMusicRecord) ->Void

class MCMusicPlayVc: MCBaseVc {
    
    var segmentedDataSource:JXSegmentedTitleDataSource!
    let segmentedView = JXSegmentedView()
    lazy var listContainerView:JXSegmentedListContainerView! = {
        return JXSegmentedListContainerView(dataSource: self)
    }()
    
    private var dataList = [String]()
    
    var musicPlayInfoLastInfo:musicPlayInfoLastBlock?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        self.navigationItem.title = "选择背景音乐"
        //scrollView 滚动页面
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.isTitleColorGradientEnabled = true
        
        segmentedView.dataSource = segmentedDataSource
        segmentedView.delegate = self
        
       
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorWidth = 20
        segmentedView.indicators = [indicator]
        view.addSubview(segmentedView)
        
        segmentedView.listContainer = listContainerView
        
        view.addSubview(listContainerView)
        
        segmentedView.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(0)
            make.right.equalTo(view.snp.right).offset(0)
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(0.0)
            make.height.equalTo(50.auto())
        }
        
        listContainerView.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(0)
            make.right.equalTo(view.snp.right).offset(0)
            make.top.equalTo(segmentedView.snp.bottom).offset(0.0)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(0.0)
        }
        
        self.getMusitType()
        
    }
    
    func getMusitType() -> Void {
        NetWorkRequest(MCMusicApi.getMusicTypeApi) { ResponseModel in
            var categorylist = Mapper<MCdietCategoryModel>().mapArray(JSONString: ResponseModel.dataString)
            self.dataList = (categorylist?.map({ MCdietCategoryModel in
                return MCdietCategoryModel.labelName ?? ""
            }))!
            self.dataList.insert("全部", at: 0)
            self.segmentedDataSource.titles = self.dataList
            self.segmentedView.reloadDataWithoutListContainer()
            self.segmentedView.reloadData()
        } failureCallback: { ResponseModel in
            self.view.makeToast("网络失败",position: .center)
        }
    }
    
    
    
}

extension MCMusicPlayVc:JXSegmentedViewDelegate {
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {

    }
}

extension MCMusicPlayVc:JXSegmentedListContainerViewDataSource {
    func numberOfLists(in listContainerView:JXSegmentedListContainerView) -> Int {
        return self.dataList.count
    }
    
    func listContainerView(_ listContainerView:JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        let dietBaseVc = MCMusicPlaybaseListVc.init()
        dietBaseVc.musictIndexType = index
        dietBaseVc.musicPlayBlock = {[weak self](music:MCMusicRecord) in
           if self?.musicPlayInfoLastInfo != nil {
               self?.musicPlayInfoLastInfo!(music)
            }
        }
        return dietBaseVc
    }
    
    
}
